
/**
 * Pops up Add Buster form
 */
function addBuster__MODULE_ID__() {

   var titleHtml = "<div>" +
                   "Add Buster" +
                   "</div>";

   var bodyHtml =  "<table id='popup_table__mc__'>" +
                   // Recipe Info
                   "<tr>" + 
                   "<td></td>" +
                   "<td id='popup-rc__mc__'>__MSG_fill_buster_info__</td>" +
                   "</tr>" +
                   // Title Errors
                   "<tr class='form-error__mc__' id='title_req__mc__'>" +
                   "<td></td>" +
                   "<td>__MSG_title_required__</td>" +
                   "</tr>" +
                   // Title Field
                   "<tr>" +
                   "<td id='popup-lc__mc__'>__MSG_title__</td>" +
                   "<td id='popup-rc__mc__'>" + 
                   "<input type='text' id='title__mc__'/>" + 
                   "</td>" +
                   "</tr>" +
                   // Description Errors
                   "<tr class='form-error__mc__' id='description_req__mc__'>" +
                   "<td></td>" +
                   "<td>__MSG_desc_required__</td>" +
                   "</tr>" +
                   // Description Field
                   "<tr>" +
                   "<td id='popup-lc__mc__'>__MSG_desc__</td>" +
                   "<td id='popup-rc__mc__'>" +
                   "<textarea rows='3' cols='30' id='description___mc__'></textarea>" +
                   "</td>" +
                   "</tr>" +
                   //Price Field
                   "<tr>" +
                   "<td id='popup-lc_add_price'>__MSG_price__</td>" +
                   "<td id='popup-rc_add_price'>" +
                   "<textarea rows='1' cols='5' id='price'></textarea>" +
                   " __MSG_per__  " + "<textarea rows='1' cols='7' id='price_per'>__MSG_item__</textarea>"
                   "</td>" +
                   "</tr>" +
                  //Price type
                  //Event date range
                  "<tr>" +
                  "<td id='popup-lc_date_range'>__MSG_date_range__</td>" +
                  "<td id='popup-rc_date_range'>" +
                  "<textarea rows='1' cols='10' id='date'>mm/dd/yyyy</textarea>" +
                  "</td>"
                  "<td id='popup-lc_time'>__MSG_time__</td> +
                  "<td id='popup-rc_time'>" + 
                  "<textarea rows='1' cols='15'>hh:mm am </textarea>" +
                  "</td>" +
                  "</tr>" +
                  //Rating
                  "<tr>" +
                  "<td id='popup-lc_rating'>__MSG_rating__</td>" +
                  "<td id='popup-rc_rating'>" +
                  "<select id=popup-rc_rating_select>" +
                  "<option value='5'>__MSG_excellent__</option>" +
                  "<option value='4'>__MSG_good__</option>" +
                  "<option value='3'>__MSG_average__</option>" +
                  "<option value='2'>__MSG_below_average__</option>" +
                  "<option value='1'>__MSG_poor__</option>" +
                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
 //                 "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "</select>"

                  //Event type
//                  "<tr>" +
//                  "<td id='popup-lc_event_type'>__MSG_event_type__</td>" +
//                  "<td id='popup-rc_event_type'>" +
//                  "<select id=popup-rc_event_type_select>" + 
//                  "<option value='ballet'></option>" + 
//                  "<option value='business'></option>" +
//                  "<option value='comedy'></option>" +
//                  "<option value=''></option>" + 
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "<option value=''></option>" +
 //                 "<option value=''></option>" +
//                  "<option value=''></option>" +
//                  "</select>"
  
                   // Buttons
                   "<tr>" +
                   "<td id='form-buttons__mc__' colspan='2'>" +
                   "<button onclick='submitAddRecipe__MODULE_ID__();'>" +
                   "__MSG_submit__</button>&nbsp;" +
                   "<button onclick='_IG_PU_hide(0);'>__MSG_cancel__</button>" +
                   "</td>" +
                   "</tr>" +
                   "</table>";

    var buttonsHtml = [""];
    
    _IG_PU_open(titleHtml, bodyHtml, buttonsHtml, "");

    // Remove White Space below form
    var popup_table = document.getElementById('popup_table__mc__');
    popup_table.nextSibling.style.display = 'none';
    
    IE6PopupFix__MODULE_ID__(popup_table); // IE6 fix for popup width
    
    _gel("title__mc__").focus();
}

/**
 * Clear Add Buster error fields
 */
function clearAddBusterErrors__MODULE_ID__() {
  
  // Get errors fields
  var title_req = document.getElementById('title_req__mc__');
  var description_req = document.getElementById('description_req__mc__');
  var ingredients_req = document.getElementById('ingredients_req__mc__');
  var servings_req = document.getElementById('servings_req__mc__');
  var servings_val = document.getElementById('servings_val__mc__');
  
  title_req.style.display = 'none';
  description_req.style.display = 'none';
  ingredients_req.style.display = 'none';
  servings_req.style.display = 'none';
  servings_val.style.display = 'none';
}

/**
 * Checks Add Buster form fields and submits data
 */
function submitAddBuster__MODULE_ID__() {
  
  // Clear all errors
  clearAddBusterErrors__MODULE_ID__(); 
  
  var detError = 0; // flag for error detected

  // Fixes cross-platform problem
  var displayValue = '';
  // Internet Explorer
  if (navigator.appName == 'Microsoft Internet Explorer') {
    displayValue = 'block';
  }
  // Other Browsers
  else {
    displayValue = 'table-row';
  }
  
  // Get errors fields
  var title_req = document.getElementById('title_req__mc__');
  var description_req = document.getElementById('description_req__mc__');
  var ingredients_req = document.getElementById('ingredients_req__mc__');
  var servings_req = document.getElementById('servings_req__mc__');
  var servings_val = document.getElementById('servings_val__mc__');

  // Check title field
  var title = document.getElementById('title__mc__');
  if (title.value == "") {
    title_req.style.display = displayValue;
    detError = 1;
  }

  // Check description field
  var description_ = document.getElementById('description___mc__');
  if (description_.value == "") {
    description_req.style.display = displayValue;
    detError = 1;
  }

  // Check ingredients field
  var ingredients = document.getElementById('ingredients__mc__');
  var ing_arr = ingredients.value.split("\n");
  var ing_valid = 0;
  ingredients.value = "";

  // Fix Internet Explorer and Opera crazy bug with str.split()
  if (navigator.appName == "Microsoft Internet Explorer" || navigator.appName == "Opera") {
    for (var i = 0; i < ing_arr.length - 1; i++) {
      if (ing_arr[i].length > 1) {
        if (ing_valid != 0) {
          ingredients.value += "\n";
        }
        ing_valid++;
        ingredients.value += ing_arr[i].substr(0, ing_arr[i].length - 1);
      }
    }
    
    
    if (ing_arr[ing_arr.length-1].length > 0) {
        if (ing_valid != 0) {
          ingredients.value += "\n";
        }
        ing_valid++;
        ingredients.value += ing_arr[i];
    }
  } else {
    for (var i = 0; i < ing_arr.length; i++) {
      if (ing_arr[i].length != 0) {
        if (ing_valid != 0) {
          ingredients.value += "\n";
        }
        ing_valid++;
        ingredients.value += ing_arr[i];
      }
    }
  }  
  
  if (ing_valid < 2) {
    ingredients_req.style.display = displayValue;
    detError = 1;
  }
  
  // Check servings field
  var servings = document.getElementById('servings__mc__');
  if (servings.value == "") {
    servings_req.style.display = displayValue;
    detError = 1;
  } else if (isNaN(servings.value)) {
    servings_val.style.display = displayValue;
    detError = 1;
  }
  
  // Get cuisine field
  var cuisine = document.getElementById('cuisine__mc__');
  
  // Case no errors detected
  if (detError == 0) {

    var reqParams = "&ti=" + escape(encodeURI(title.value)) + 
                    "&de=" + escape(encodeURI(description_.value)) + 
                    "&in=" + escape(encodeURI(ingredients.value)) + 
                    "&cu=" + escape(encodeURI(cuisine.value)) + 
                    "&se=" + escape(encodeURI(servings.value));

    var reqUrl = MYBUSTER_URL__MODULE_ID__ + "?action=add&token="
                 + token__MODULE_ID__ + reqParams;

    // Disable form fields
    title.disabled = true;
    description_.disabled = true;
    ingredients.disabled = true;
    cuisine.disabled = true;
    servings.disabled = true;

    // Loading...
    var form_buttons = document.getElementById('form-buttons__mc__');
    form_buttons.innerHTML = "<img src='" + loading__MODULE_ID__ + "' alt='' />";

    _IG_FetchXmlContent(reqUrl, function (response) {
      var responseStatus = "";
      if (response == null || typeof(response) != "object" || response.firstChild == null) {
        form_buttons.innerHTML = "__MSG_adding_buster_failed__<br />";
        responseStatus = 'failure';
      } else {
        var responseXml = response.getElementsByTagName("response").item(0);
        responseStatus = responseXml.getElementsByTagName("status").item(0).firstChild.nodeValue;
      }
      // Display confirmation message
      var buttonMsg = "";
      if (responseStatus == 'success') {
        // Display Recipe Added Message
        buttonMsg += "__MSG_buster_added__<br />__MSG_buster_may_take_some_time__<br />";
        // Update MyRecipes tab in 5 secs
        setTimeout(function() {
			myRecipesUpdated__MODULE_ID__ = 1;
			fetchMyRecipes__MODULE_ID__();
		}, 
		5000);
      } else if (responseStatus == 'authentication failure'){
        buttonMsg += "__MSG_sending_buster_failed__<br />";
        cancelAuthorization__MODULE_ID__();
      } else {
        buttonMsg += "__MSG_sending_buster_failed__<br />";
      }

      buttonMsg += "<button onclick='_IG_PU_hide(0)'>__MSG_close__</button>";
      form_buttons.innerHTML = buttonMsg;

    }, { refreshInterval: 0 });
  }
}
